﻿using ExecutiveDashboard_SL1;
//using ExecutiveDashboard_SL1.Web.DomainServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.TimeBar;

namespace ExecutiveDashboard_SL1
{
    public class RadTimeBarBehavior : Behavior<RadTimeBar>
    {
        public ViewModel AssociatedViewModel
        {
            get
            {
                return this.AssociatedObject.DataContext as ViewModel;
            }
        }

        private static void OnCurrentItemIntervalPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ////AWDWDomainContext context = new AWDWDomainContext();
            //var timeBar = (d as RadTimeBarBehavior).AssociatedObject;
            //string interval = string.Empty;

            //EntityQuery<SalesAmountExtract> query = null;

            //if (e.NewValue is WeekInterval || e.NewValue is MonthInterval)
            //{
            //    interval = "day";
            //    query = context.GetDailySalesAmountsQuery();
            //}
            //else
            //{
            //    query = context.GetMonthlySalesAmountsQuery();
            //}

            ////var query = context.GetSalesAmountsForPeriodQuery(timeBar.PeriodStart, timeBar.PeriodEnd, interval);
            //context.Load(query).Completed += (s, args) =>
            //{
            //    var resultsFromDB = (s as LoadOperation<SalesAmountExtract>).Entities.ToList();
            //    List<SalesAmountExtractWrapper> data = new List<SalesAmountExtractWrapper>();

            //    if (interval == "day")
            //    {
            //        for (DateTime day = timeBar.PeriodStart; day <= timeBar.PeriodEnd; day = day.AddDays(1))
            //        {
            //            if (!resultsFromDB.Any(r => r.StartDate.Equals(day)))
            //            {
            //                //var newRandomDecimalValue = new Random().Next(min, max);
            //                SalesAmountExtract previousDayExtract = resultsFromDB.FirstOrDefault(r => r.StartDate.Equals(day.AddDays(-1)));
            //                if (previousDayExtract != null)
            //                {
            //                    var itemToAdd = new SalesAmountExtract { StartDate = day, ActualValue = previousDayExtract.ActualValue, TargetValue = previousDayExtract.TargetValue };
            //                    resultsFromDB.Add(itemToAdd);
            //                }
            //            }
            //        }
            //    }

            //    data.AddRange(from all in resultsFromDB orderby all.StartDate select new SalesAmountExtractWrapper(all));
            //    (d as RadTimeBarBehavior).AssociatedViewModel.ActualTrendData = data;
            //};
        }

        public IntervalBase CurrentItemInterval
        {
            get
            {
                return (IntervalBase)GetValue(CurrentItemIntervalProperty);
            }
            set
            {
                SetValue(CurrentItemIntervalProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for CurrentItemInterval.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentItemIntervalProperty =
        DependencyProperty.Register("CurrentItemInterval", typeof(IntervalBase), typeof(RadTimeBarBehavior), new PropertyMetadata(null, OnCurrentItemIntervalPropertyChanged));

        protected override void OnAttached()
        {
            base.OnAttached();
            
            this.AssociatedObject.SelectionChanged += new Telerik.Windows.RadRoutedEventHandler(AssociatedObject_SelectionChanged);
        }
  
        void AssociatedObject_SelectionChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            this.UpdateChartRangeSettings();
            this.UpdateChartPointMarksSettings();
        }

        private void UpdateChartRangeSettings()
        {
            int selectedDaysCount = (this.AssociatedObject.SelectionEnd - this.AssociatedObject.SelectionStart).Days;
            
            this.AssociatedViewModel.IsAxisXAutoRange = false;
            this.AssociatedViewModel.AxisXMinValue = this.AssociatedObject.SelectionStart.ToOADate();
            this.AssociatedViewModel.AxisXMaxValue = this.AssociatedObject.SelectionEnd.ToOADate();

            if (selectedDaysCount <= 14)
            {
                this.AssociatedViewModel.AxisXStep = 1;
                this.AssociatedViewModel.AxisXDefaultLabelFormat = "ddd dd.MM";
            }
            else if (selectedDaysCount <= 31)
            {
                this.AssociatedViewModel.AxisXStep = 2;
                this.AssociatedViewModel.AxisXDefaultLabelFormat = "dd.MM";
            }
            else
            {
                this.AssociatedViewModel.AxisXStep = (int)(selectedDaysCount / 15);
                this.AssociatedViewModel.AxisXDefaultLabelFormat = "dd.MM";
            }
        }

        private void UpdateChartPointMarksSettings()
        {
            int selectedDaysCount = (this.AssociatedObject.SelectionEnd - this.AssociatedObject.SelectionStart).Days;

            this.AssociatedViewModel.ShowPointMarks = selectedDaysCount < 200;
        }
    }
}